import { Injectable, UnauthorizedException } from '@nestjs/common';
import { PassportStrategy } from '@nestjs/passport';
import { Strategy, ExtractJwt } from 'passport-jwt';
import { AccountEntity } from 'src/modules/system/account/entities/account.entity';

@Injectable()
export class JwtStrategy extends PassportStrategy(Strategy) {
    constructor () {
        super({
            jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
            ignoreExpiration: false,
            secretOrKey: 'abc',
        });
    }

    async validate(payload: { id: number, username: string }) {
        console.log('进来了', payload);
        const account = await AccountEntity.findOne(payload.id);
        if (!account) {
            throw new UnauthorizedException('当前账号不存在');
        }
        return account;
    }
}
